<template>
  <div>
    <div class="ggg">
      <!-- <p v-show="sort == 0">升序</p>
      <p v-show="sort == 1">降序</p>-->
      <p @click="changeSort">
        价格
        <van-icon name="arrow-up" v-show="sort==1" />
        <van-icon name="arrow-down" v-show="sort==2" />
      </p>
    </div>
    <div class="room">
      <!-- <van-card
       v-for="(item,index) in list" :key="index"
        num="2"
        :price="item.productName"
        :title="item.salePrice"
        :thumb="'/img/'+item.productImage">
        <p class="pp" @click="addCart">+</p>
      </van-card>-->

      <div class="fyh" v-for="(item,index) in fyhlist" :key="index">
        <!-- <img src="" alt /> -->
        <img :src="'/img/'+item.productImage" alt />
        <div class="toom">
          <p>{{item.productName}}</p>
          <p>￥{{item.salePrice}}</p>
          <p>166人付款</p>
          <p class="pp" @click="addCart(item)">+</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      sort: 0,
    };
  },
  computed: {
    // 排序
    fyhlist() {
      let lists = this.list;
      lists.sort((a, b) => {
        if (this.sort == 1) {
          return a.salePrice - b.salePrice;
        } else if (this.sort == 2) {
          return b.salePrice - a.salePrice;
        }
      });
      return lists;
    },
  },
  created() {},
  mounted() {
    this.gouwu();
  },
  methods: {
    gouwu() {
      this.$axios({
        url: "/data.json",

      }).then((res) => {
        console.log(res);
        this.list = res.result.list;
      });
    },
    addCart(good) {
      let goods = this.$store.state.m_cartList;

      let index = goods.findIndex((item) => {
        return item._id == good._id;
      });
      //购物车中存在当前商品
      if (index == -1) {
        good.num = 1;
        goods.push(good);
      } else {
        goods.map((item) => {
          if (item._id == good._id) {
            item.num++;
          }
        });
      }
      this.$store.commit("addCart", goods);
      this.$router.push("/gouwu");
    },
    changeSort() {
      if (this.sort == 0 || this.sort == 2) {
        this.sort = 1;
      } else {
        this.sort = 2;
      }
    },
  },
  pai() {},
};
</script>

<style scoped lang="less">
.room {
  margin-top: 0.3rem;
}
.ggg {
  display: flex;
  height: 0.5rem;
  margin-top: 0.2rem;
  font-size: 0.32rem;
}
.fyh {
  width: 100%;
  height: 1.2rem;
  display: flex;
  background-color: #ffffff;
  margin-bottom: 0.2rem;
  img {
    width: 1rem;
    height: 0.8rem;
  }
  .toom {
    width: 100%;
    position: relative;
    p {
      font-size: 0.2rem;
    }
    :nth-of-type(1) {
      font-size: 0.2rem;
    }
    :nth-of-type(2) {
      font-size: 0.2rem;
      color: #ac87f2;
    }
    :nth-of-type(3) {
      margin-top: 0.2rem;
    }
    .pp {
      position: absolute;
      right: 0.3rem;
      bottom: 0.2rem;
      width: 0.5rem;
      height: 0.5rem;
      border-radius: 50%;
      color: #ffffff;
      background-color: #ac87f2;
      font-size: 0.5rem;
      text-align: center;
      line-height: 0.5rem;
    }
  }
}
</style>
